#include <iostream>
#include <vector>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution 
{
    bool isMirror(TreeNode * node1, TreeNode * node2)
    {
        bool result = true;
        if(node1 == nullptr && node2 == nullptr)
        {
            return true;
        }
        else if (node1 == nullptr || node2 ==nullptr)
        {
            return false;
        }
        
        if(node1->val != node2->val)
        {
            return false;
        }
        else if (node1->left != nullptr && node2->right != nullptr && node1->left->val != node2->right->val)
        {
            return false;
        }
        else if (node1->right != nullptr && node2->left != nullptr && node1->right->val != node2->left->val)
        {
            return false;
        }
        result = isMirror(node1->left, node2->right) & isMirror(node1->right, node2->left);
        return result;
    }
public:
    bool isSymmetric(TreeNode* root) 
    {
        if(root == nullptr)
        {
            return true;
        }
        if(root->left == nullptr && root->right ==nullptr)
        {
            return true;
        }
        return isMirror(root->left, root->right);
    }
};

// 辅助函数：创建二叉树节点
TreeNode* createNode(int val) {
    return new TreeNode(val);
}

// 辅助函数：释放二叉树内存
void deleteTree(TreeNode* root) {
    if (root == nullptr) return;
    deleteTree(root->left);
    deleteTree(root->right);
    delete root;
}

int main() {
    Solution solution;
    
    // 测试案例1: LeetCode示例1 - [1,2,2,3,4,4,3] (对称)
    //       1
    //      / \
    //     2   2
    //    / \ / \
    //   3  4 4  3
    // 预期输出: true
    cout << "测试案例1: [1,2,2,3,4,4,3] (对称)" << endl;
    TreeNode* root1 = createNode(1);
    root1->left = createNode(2);
    root1->right = createNode(2);
    root1->left->left = createNode(3);
    root1->left->right = createNode(4);
    root1->right->left = createNode(4);
    root1->right->right = createNode(3);
    
    bool result1 = solution.isSymmetric(root1);
    cout << "输出: " << (result1 ? "true" : "false") << endl;
    cout << "预期: true" << endl << endl;
    
    // 测试案例2: LeetCode示例2 - [1,2,2,null,3,null,3] (不对称)
    //       1
    //      / \
    //     2   2
    //      \   \
    //       3   3
    // 预期输出: false
    cout << "测试案例2: [1,2,2,null,3,null,3] (不对称)" << endl;
    TreeNode* root2 = createNode(1);
    root2->left = createNode(2);
    root2->right = createNode(2);
    root2->left->right = createNode(3);
    root2->right->right = createNode(3);
    
    bool result2 = solution.isSymmetric(root2);
    cout << "输出: " << (result2 ? "true" : "false") << endl;
    cout << "预期: false" << endl << endl;
    
    // 测试案例3: 空树
    cout << "测试案例3: [] (空树)" << endl;
    TreeNode* root3 = nullptr;
    
    bool result3 = solution.isSymmetric(root3);
    cout << "输出: " << (result3 ? "true" : "false") << endl;
    cout << "预期: true" << endl << endl;
    
    // 测试案例4: 只有一个节点
    cout << "测试案例4: [1] (单节点)" << endl;
    TreeNode* root4 = createNode(1);
    
    bool result4 = solution.isSymmetric(root4);
    cout << "输出: " << (result4 ? "true" : "false") << endl;
    cout << "预期: true" << endl << endl;
    
    // 测试案例5: 完全对称的三层树
    //       1
    //      / \
    //     2   2
    //    / \ / \
    //   3  4 4  3
    //  /|  |\ /|  |\
    // 5 6  7 8 8 7 6 5
    cout << "测试案例5: 完全对称的三层树" << endl;
    TreeNode* root5 = createNode(1);
    root5->left = createNode(2);
    root5->right = createNode(2);
    root5->left->left = createNode(3);
    root5->left->right = createNode(4);
    root5->right->left = createNode(4);
    root5->right->right = createNode(3);
    
    bool result5 = solution.isSymmetric(root5);
    cout << "输出: " << (result5 ? "true" : "false") << endl;
    cout << "预期: true" << endl << endl;
    
    // 测试案例6: 值相同但结构不对称
    //       1
    //      / \
    //     2   2
    //    /     \
    //   3       3
    // 预期输出: false
    cout << "测试案例6: [1,2,2,3,null,null,3] (值相同但结构不对称)" << endl;
    TreeNode* root6 = createNode(1);
    root6->left = createNode(2);
    root6->right = createNode(2);
    root6->left->left = createNode(3);
    root6->right->right = createNode(3);
    
    bool result6 = solution.isSymmetric(root6);
    cout << "输出: " << (result6 ? "true" : "false") << endl;
    cout << "预期: false" << endl << endl;
    
    // 测试案例7: 左右子树都为空但根节点不为空
    //   1
    // 预期输出: true
    cout << "测试案例7: [1,null,null] (只有根节点)" << endl;
    TreeNode* root7 = createNode(1);
    
    bool result7 = solution.isSymmetric(root7);
    cout << "输出: " << (result7 ? "true" : "false") << endl;
    cout << "预期: true" << endl << endl;
    
    // 测试案例8: 不对称的值
    //       1
    //      / \
    //     2   3
    // 预期输出: false
    cout << "测试案例8: [1,2,3] (不对称的值)" << endl;
    TreeNode* root8 = createNode(1);
    root8->left = createNode(2);
    root8->right = createNode(3);
    
    bool result8 = solution.isSymmetric(root8);
    cout << "输出: " << (result8 ? "true" : "false") << endl;
    cout << "预期: false" << endl << endl;
    
    // 清理内存
    deleteTree(root1);
    deleteTree(root2);
    deleteTree(root4);
    deleteTree(root5);
    deleteTree(root6);
    deleteTree(root7);
    deleteTree(root8);
    // root3 是 nullptr，不需要删除
    
    return 0;
}